package com.yuanyu.server.impl;

import com.yuanyu.common.util.AssertUtil;
import com.yuanyu.common.util.CopyUtil;
import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.mapper.mysqlMapper.Pump_maintenance_and_disassembly_report.Pump_maintenance_and_disassembly_report_1_Mapper;
import com.yuanyu.mapper.mysqlMapper.Pump_maintenance_and_disassembly_report.Pump_maintenance_and_disassembly_report_Mapper;
import com.yuanyu.model.dto.Pump_maintenance_and_disassembly_reportDTO;
import com.yuanyu.model.entity.Pump_maintenance_and_disassembly_report.Pump_maintenance_and_disassembly_report;
import com.yuanyu.model.entity.Pump_maintenance_and_disassembly_report.Pump_maintenance_and_disassembly_report_1;
import com.yuanyu.model.vo.Pump_maintenance_and_disassembly_reportVO;
import com.yuanyu.server.ITestPart3Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class TestPart3ServerImpl implements ITestPart3Server{
    @Autowired
    private Pump_maintenance_and_disassembly_report_Mapper pumpMaintenanceAndDisassemblyReportMapper;
    @Autowired
    private Pump_maintenance_and_disassembly_report_1_Mapper pumpMaintenanceAndDisassemblyReport1Mapper;
    @Autowired
    private MainTable_Mapper mainTableMapper;


    @Override
    public String test() {
        return "测试成功";
    }

    @Override
    public String test3(Pump_maintenance_and_disassembly_reportVO pumpMaintenanceAndDisassemblyReportVO) {
        //先判断Case是否存在
        Pump_maintenance_and_disassembly_report test = pumpMaintenanceAndDisassemblyReportMapper.selTableTime(pumpMaintenanceAndDisassemblyReportVO.getInput_Case());
        AssertUtil.isNull(test,"Case已存在,如需修改内容请前往后台修改");
        //时间表
        Pump_maintenance_and_disassembly_report pumpMaintenanceAndDisassemblyReport = new Pump_maintenance_and_disassembly_report();
        //主体表
        Pump_maintenance_and_disassembly_report_1 pumpMaintenanceAndDisassemblyReport1 = new Pump_maintenance_and_disassembly_report_1();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReportVO,pumpMaintenanceAndDisassemblyReport);
        //时间表 获取当前时间
        pumpMaintenanceAndDisassemblyReport.setCreatedAt(LocalDateTime.now());
        pumpMaintenanceAndDisassemblyReport.setUpdatedAt(LocalDateTime.now());
        //主体表 获取VO类对应的值
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReportVO,pumpMaintenanceAndDisassemblyReport1);

        System.out.println(pumpMaintenanceAndDisassemblyReport);
        System.out.println(pumpMaintenanceAndDisassemblyReport1);

        //判断是否出现异常,出现异常返回"添加数据出现异常,请检查",并且回滚数据
        int i = pumpMaintenanceAndDisassemblyReportMapper.addTableTime(pumpMaintenanceAndDisassemblyReport);
        int i1 = pumpMaintenanceAndDisassemblyReport1Mapper.addTable1(pumpMaintenanceAndDisassemblyReport1);
        AssertUtil.isTrue(i == 1 && i1 == 1,"添加数据出现异常,请检查");
        mainTableMapper.updateByCaseFormStartTime(pumpMaintenanceAndDisassemblyReportVO.getInput_Case());
        return "添加成功";
    }

    @Override
    public Pump_maintenance_and_disassembly_reportDTO getTest3(String Case) {
        //获取每个子表的信息,并判断,如果不存在就返回"Case不存在"
        Pump_maintenance_and_disassembly_report pumpMaintenanceAndDisassemblyReport = pumpMaintenanceAndDisassemblyReportMapper.selTableTime(Case);
        if (pumpMaintenanceAndDisassemblyReport == null)return null;
        Pump_maintenance_and_disassembly_report_1 pumpMaintenanceAndDisassemblyReport1 = pumpMaintenanceAndDisassemblyReport1Mapper.selTable1(Case);

        Pump_maintenance_and_disassembly_reportDTO pumpMaintenanceAndDisassemblyReportDTO = new Pump_maintenance_and_disassembly_reportDTO();
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReport,pumpMaintenanceAndDisassemblyReportDTO);
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReport1,pumpMaintenanceAndDisassemblyReportDTO);
        return pumpMaintenanceAndDisassemblyReportDTO;
    }

    @Override
    public String updateTable3(Pump_maintenance_and_disassembly_reportVO pumpMaintenanceAndDisassemblyReportVO) {
        //时间表
        Pump_maintenance_and_disassembly_report pumpMaintenanceAndDisassemblyReport = new Pump_maintenance_and_disassembly_report();
        //主体表
        Pump_maintenance_and_disassembly_report_1 pumpMaintenanceAndDisassemblyReport1 = new Pump_maintenance_and_disassembly_report_1();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReportVO,pumpMaintenanceAndDisassemblyReport);
        //时间表 获取当前时间
        pumpMaintenanceAndDisassemblyReport.setUpdatedAt(LocalDateTime.now());
        //主体表 获取VO类对应的值
        CopyUtil.copyClass(pumpMaintenanceAndDisassemblyReportVO,pumpMaintenanceAndDisassemblyReport1);

        int i = pumpMaintenanceAndDisassemblyReportMapper.updateTableTime(pumpMaintenanceAndDisassemblyReport);
        int i1 = pumpMaintenanceAndDisassemblyReport1Mapper.updateTable1(pumpMaintenanceAndDisassemblyReport1);
        AssertUtil.isTrue(i == 1 && i1 == 1,"表3修改数据出现异常,请检查");
        return "修改成功";
    }
}
